<template>
  <div id="detail">
    <detail-nav-bar @titleClick="titleClick" ref="DetailNav"></detail-nav-bar>
    <scroll class="detail-scroll" ref="scroll" :probe-type="3" @scroll="contentScroll">
      <detail-swiper :top-images="topImages"></detail-swiper>
      <detail-base-info :goods="goods"></detail-base-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad"></detail-goods-info>
      <detail-params :param-info="paramsInfo" ref="params"></detail-params>
      <detail-comment-info :comment-info="commentInfo" ref="comment" />
      <goods-list :goods="recommends" ref="recommend" />
    </scroll>
    <detail-bottom-bar @addCart="addToCart" @scbtn="scbtn"></detail-bottom-bar>
    <back-top @click.native="backClick" v-show="isShowBackTop" />
    <!-- 自己封装 <toast :message="message" :show="show"></toast> -->
  </div>
</template>

<script>
import Scroll from '@/components/common/scroll/Scroll.vue'
import GoodsList from '@/components/content/goods/GoodsList.vue'

import DetailNavBar from './childComps/DetailNavBar.vue'
import DetailSwiper from './childComps/DetailSwiper.vue'
import DetailBaseInfo from './childComps/DetailBaseInfo.vue'
import DetailShopInfo from './childComps/DetailShopInfo.vue'
import DetailGoodsInfo from './childComps/DetailGoodsInfo.vue'
import DetailParams from './childComps/DetailParams.vue'
import DetailCommentInfo from './childComps/DetailCommentInfo.vue'
import DetailBottomBar from './childComps/DetailBottomBar.vue'

import { getDetail, getRecommend, Goods, GoodsParam, Shop } from '@/network/detail'
import { itemListenerMixin, backTopMixin } from '@/utils/mixin'
import { debounce } from '@/utils/utils'
import { mapActions } from 'vuex'
// import Toast from '@/components/common/toast/Toast.vue'

export default {
  name: 'Detail',
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailParams,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar
  },
  // 混入的操作
  mixins: [itemListenerMixin, backTopMixin],
  data() {
    return {
      iid: null, // 传参的iid
      topImages: [], // 详情页轮播图
      goods: {}, // 默认是空对象
      shop: {},
      detailInfo: {},
      paramsInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [],
      getThemeTopY: null,
      currentIndex: 0,
      temImgListener: null
      // message: '',
      // show: false
    }
  },
  created() {
    // 获取新的iid
    // 请求需要的参数 将参数进行数据格式化的操作 需要进一步进行数据的封装 防止数据懒加载以后轮播图获取不到数据
    // 导致页面进行刷新假死 造成轮播图只展示一张图片的操作
    // 参数传递 iid
    this.iid = this.$route.params.iid
    getDetail(this.iid).then(res => {
      const data = res.result
      // 轮播图
      this.topImages = data.itemInfo.topImages
      // 商品6`
      this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)
      // 店铺信息
      this.shop = new Shop(data.shopInfo)
      // 参数信息
      this.detailInfo = data.detailInfo
      // 获取参数信息
      this.paramsInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)
      // 获取评论信息
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0]
      }
    })

    getRecommend().then(res => {
      this.recommends = res.data.list
    })
    // 4. 给getThemeTopY赋值 给函数赋值进行防抖
    this.getThemeTopY = debounce(() => {
      this.themeTopYs = []
      this.themeTopYs.push(0)
      this.themeTopYs.push(this.$refs.params.$el.offsetTop - 35)
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop - 35)
      this.themeTopYs.push(Number.MAX_VALUE)
    }, 100)
  },
  methods: {
    // 映射方法 将方法从里面抽取出来
    ...mapActions(['addCart', 'scList']),
    imageLoad() {
      this.refresh()
      // 等图片加载 然后获取高度
      this.getThemeTopY()
    },
    titleClick(index) {
      // 监听标题的点击
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 200)
    },
    contentScroll(position) {
      // 拿到y值
      const positionY = -position.y
      // 和主题的值进行对比 这样就不用考虑i的类型
      let length = this.themeTopYs.length
      for (let i = 0; i < length - 1; i++) {
        if (this.currentIndex !== i && positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i + 1]) {
          this.currentIndex = i
          // 将navbar进行位置的变换 然后就可以进行变换
          this.$refs.DetailNav.currentIndex = this.currentIndex
        }
        this.ListenerShowBackTop(position)
      }
    },
    addToCart() {
      // console.log('监听到子组件的事件')
      // console.log('加入的商品id:' + this.iid)
      // 获取购物车信息
      const product = {}
      // 1 图片
      product.image = this.topImages[0]
      // 2 标题
      product.title = this.goods.title
      // 3 描述
      product.desc = this.goods.desc
      // 4 最新的价格
      product.price = this.goods.newPrice
      // 5 商品id
      product.iid = this.iid
      // 6 复选框按钮
      product.checked = false

      // this.$store.dispatch('addCart',product)
      this.addCart(product).then(res => {
        this.$toast.show(res, 1000)
      })
    },
    scbtn() {
      // 保存状态信息 将信息存储起来
      const scproduct = {}
      // 1 图片
      scproduct.image = this.topImages[0]
      // 2 标题
      scproduct.title = this.goods.title
      // 3 描述
      scproduct.desc = this.goods.desc
      // 4 最新的价格
      scproduct.price = this.goods.newPrice
      // 5 商品id
      scproduct.iid = this.iid
      // 6 单击按钮
      scproduct.only = false

      // this.$store.dispatch('scList', scproduct)
      this.scList(scproduct).then(res => {
        this.$toast.show(res, 1000)
      })
    }
  },
  destroyed() {
    // 2.取消全局事件的监听
    this.$bus.$off('itemImageLoad', this.itemImgListener)
  },
  mounted() { }
}
</script>

<style scoped>
#detail {
  height: 100vh;
  position: relative;
  z-index: 1;
  background-color: #ffffff;
}

.detail-scroll {
  height: calc(100% - 44px - 2.09rem);
  overflow: hidden;
}
</style>
